package com.tools.util;

import java.lang.reflect.Field;

/**
 * 枚举标识与中文转换类 要求枚举类型的定义必须为key,chinese否则无法转换
 * 
 * @author Admin
 * 
 */
public class EnumUtils {

	/**
	 * 英文状态转中文
	 * 
	 * @param obj
	 *            状态枚举数组
	 * @param str
	 * @return
	 */
	public static String enumKeyToChinese(Object[] obj, String str) {
		// 判断类等于空或者不是枚举类则返回null
		String chinese = "";
		try {
			if (obj != null && obj.length > 0 && !obj[0].getClass().isEnum()) {
				return null;
			}
			for (Object o : obj) {
				Field index = o.getClass().getDeclaredField("key");
				Field name = o.getClass().getDeclaredField("chinese");
				if(index==null || name==null){
					continue;
				}
				if (index.get(o).equals(str)) {
					chinese = name.get(o).toString();
					break;
				}
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return chinese;
	}
}
